<?php


namespace app\api\model\Video;


use think\facade\Db;
use think\Model;

class VideoCommentModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "video_comment";

    /**
     * 查询一条
     * @param $where
     * @return array|Model|null
     * @throws
     */
    public static function getVideoCommentWhere($where,$filed = "vc.*")
    {
        $like = Db::name("video_comment")
            ->alias('vc')
            ->join('user u','u.id = vc.uid')
            ->field($filed)->where($where)->find();
        return $like;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getVideoCommentCountWhere($where)
    {
        $count = Db::name("video_comment")
            ->alias('vc')
            ->join('user u','u.id = vc.uid')
            ->where($where)->count('vc.id');
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getVideoCommentListWhere($page,$limit,$where = [],$field="vc.*")
    {
        $start = ceil(($page-1)*$limit);
        $list = Db::name('video_comment')
            ->alias('vc')
            ->join('user u','u.id = vc.uid')
            ->field($field)
            ->where($where)
            ->limit($start,$limit)
            ->order('vc.add_time desc')
            ->select()->toArray();
        return $list;
    }

    /**
     * 更新
     * @param $where
     * @param $save
     * @return int
     * @throws
     */
    public static function updateVideoCommentWhere($where,$save)
    {
        $flag = Db::name("video_comment")->where($where)->update($save);
        return $flag;
    }

    /**
     * 添加
     * @param $data
     * @return int|string
     */
    public static function addVideoComment($data)
    {
        $id = Db::name("video_comment")->insertGetId($data);
        return $id;
    }

}